//
//  ActionView.m
//  phmall
//
//  Created by admin on 2017/11/30.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "ActionView.h"

@implementation ActionView

-(instancetype)init{
    if (self==[super init]) {
        
        self.backgroundColor=[UIColor colorWithRed:0.38f green:0.72f blue:0.97f alpha:1.00f];
        
        self.price=[[UILabel alloc]init];
        self.price.text=@"₱999";
        self.price.textColor=[UIColor whiteColor];
        self.price.font=[UIFont systemFontOfSize:26];
        [self addSubview:self.price];
        
        self.market_price=[[UILabel alloc]init];
        self.market_price.text=@"₱9999";
        self.market_price.textColor=[UIColor whiteColor];
        self.market_price.font=[UIFont systemFontOfSize:14];
        [self addSubview:self.market_price];
        
        self.number=[[UILabel alloc]init];
        self.number.text=[NSString stringWithFormat:@"%@%@",Localized(@"剩余"),@"200"];
        self.number.textColor=[UIColor whiteColor];
        self.number.font=[UIFont systemFontOfSize:13];
        [self addSubview:self.number];
        
        self.type=[[UILabel alloc]init];
        self.type.text=Localized(@"距结束");
        self.type.textColor=[UIColor whiteColor];
        self.type.font=[UIFont systemFontOfSize:15];
        [self addSubview:self.type];
        
        self.over=[[UILabel alloc]init];
        self.over.text=@"该商品已被抢光";
        self.over.textColor=[UIColor whiteColor];
        self.over.font=[UIFont systemFontOfSize:18];
        self.over.hidden=YES;
        [self addSubview:self.over];
        
        self.clock=[[UIImageView alloc]init];
        self.clock.image=[UIImage imageNamed:@"时钟"];
        [self addSubview:self.clock];
        
        self.hourLabel=[UILabel new];
        [self  addSubview:self.hourLabel];
        self.hourLabel.backgroundColor=[UIColor clearColor];
        self.hourLabel.text=@"00";
        self.hourLabel.layer.cornerRadius=3;
        self.hourLabel.layer.masksToBounds=YES;
        self.hourLabel.font=[UIFont systemFontOfSize:15];
        self.hourLabel.textColor=[UIColor whiteColor];
        self.hourLabel.textAlignment=NSTextAlignmentCenter;
        
        
        self.colon=[UILabel new];
        self.colon.text=@":";
        self.colon.font=[UIFont systemFontOfSize:15];
        self.colon.textColor=[UIColor whiteColor];
        self.colon.textAlignment=NSTextAlignmentCenter;
        [self addSubview:self.colon];
        
        
        self.minuteLabel=[UILabel new];
        [self  addSubview:self.minuteLabel];
        self.minuteLabel.backgroundColor=[UIColor clearColor];
        self.minuteLabel.text=@"00";
        self.minuteLabel.layer.cornerRadius=3;
        self.minuteLabel.layer.masksToBounds=YES;
        self.minuteLabel.font=[UIFont systemFontOfSize:15];
        self.minuteLabel.textAlignment=NSTextAlignmentCenter;
        self.minuteLabel.textColor=[UIColor whiteColor];
        
        self.colon2=[UILabel new];
        self.colon2.text=@":";
        self.colon2.font=[UIFont systemFontOfSize:15];
        self.colon2.textColor=[UIColor whiteColor];
        self.colon2.textAlignment=NSTextAlignmentCenter;
        [self addSubview:self.colon2];
        
        self.secondLabel=[UILabel new];
        [self  addSubview:self.secondLabel];
        self.secondLabel.backgroundColor=[UIColor clearColor];
        self.secondLabel.text=@"00";
        self.secondLabel.layer.cornerRadius=3;
        self.secondLabel.layer.masksToBounds=YES;
        self.secondLabel.font=[UIFont systemFontOfSize:15];
        self.secondLabel.textAlignment=NSTextAlignmentCenter;
        self.secondLabel.textColor=[UIColor whiteColor];
        
        self.time=48000;
        [self startime];
        
        self.back=[[UIImageView alloc]init];
        [self addSubview:self.back];
        ViewBorderRadius(self.back, 3, 0, [UIColor clearColor]);
        self.back.backgroundColor=[UIColor colorWithHexString:@"#FFD800"];
      
        
        self.red=[[UIView alloc]init];
        [self addSubview:self.red];
        ViewBorderRadius(self.red, 3, 0, [UIColor clearColor]);
        self.red.backgroundColor=[UIColor colorWithHexString:@"#FCFF00"];
       
        
        self.scale=[[UILabel alloc]init];
        self.scale.text=@"已抢40%";
        self.scale.textColor=[UIColor orangeColor];
        self.scale.font=[UIFont systemFontOfSize:10];
        self.scale.textAlignment=NSTextAlignmentCenter;
        [self addSubview:self.scale];

    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    CGSize superSize = self.frame.size;
    
    if (CGSizeEqualToSize(superSize, CGSizeZero)) {
        return;
    }
    [self.price mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.centerY.offset(0);
    }];
    
    [self.market_price mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.price.mas_right).offset(10);
        make.top.offset(0);
        make.height.offset(24);
    }];
    [self.number mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.price.mas_right).offset(10);
        make.bottom.offset(0);
        make.height.offset(20);
    }];
    [self.type mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.hourLabel);
        make.right.equalTo(self.hourLabel.mas_left).with.offset(-2);
    }];
    [self.hourLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(24);
        make.right.equalTo(self.colon.mas_left).with.offset(-2);
    }];
    [self.colon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.hourLabel);
        make.right.equalTo(self.minuteLabel.mas_left).with.offset(-2);
    }];
    [self.minuteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.hourLabel);
        make.width.mas_equalTo(20);
        make.right.equalTo(self.colon2.mas_left).with.offset(-2);
    }];
    [self.colon2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.hourLabel);
        make.right.equalTo(self.secondLabel.mas_left).with.offset(-2);
    }];
    [self.secondLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.hourLabel);
        make.width.mas_equalTo(20);
        make.right.equalTo(self.clock.mas_left).with.offset(-10);
    }];
    [self.over mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.right.equalTo(self.clock.mas_left).with.offset(-5);
    }];
    [self.clock mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.width.height.offset(28);
        make.right.offset(-10).priorityHigh();
    }];
    
    [self.scale mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.back);
    }];
    [self.red mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.back);
        make.centerY.equalTo(self.back);
        make.height.equalTo(self.back);
        make.width.equalTo(self.back.mas_width).multipliedBy(0.4);
    }];
    [self.back mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-3);
        make.height.offset(14);
//        make.right.offset(-10);
        make.width.mas_equalTo(100);
         make.right.equalTo(self.clock.mas_left).with.offset(-10);
    }];
}
-(void)countTime{
    if(time<=0){ //倒计时结束，关闭
        //取消定时器
        [self.timer invalidate];
        self.timer = nil;
    }else{
        int days = (int)(self.time/(3600*24));
        if (days==0) {
            
        }
        int hours = (int)(self.time-days*24*3600)/3600;
        int minute = (int)(self.time-days*24*3600-hours*3600)/60;
        int second = (int)self.time-days*24*3600-hours*3600-minute*60;
        
        dispatch_async(dispatch_get_main_queue(), ^{
            if (days==0) {
                
            }else{
                
            }
            if (hours<10) {
                self.hourLabel.text = [NSString stringWithFormat:@"0%d",hours];
            }else{
                self.hourLabel.text = [NSString stringWithFormat:@"%d",hours];
            }
            if (minute<10) {
                self.minuteLabel.text = [NSString stringWithFormat:@"0%d",minute];
            }else{
                self.minuteLabel.text = [NSString stringWithFormat:@"%d",minute];
            }
            if (second<10) {
                self.secondLabel.text = [NSString stringWithFormat:@"0%d",second];
            }else{
                self.secondLabel.text = [NSString stringWithFormat:@"%d",second];
            }
            
        });
        self.time=self.time-1;
    }
}
-(void)hiddeLanel{
    self.hourLabel.hidden=YES;
    self.minuteLabel.hidden=YES;
    self.secondLabel.hidden=YES;
    self.colon2.hidden=YES;
    self.colon.hidden=YES;
    self.back.hidden=YES;
    self.type.hidden=YES;
    self.red.hidden=YES;
    self.scale.hidden=YES;
    self.over.hidden=NO;
}
-(void)startime{
    self.timer=[NSTimer timerWithTimeInterval:1 target:self selector:@selector(countTime) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
@end
